﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;

namespace GRWL.BZSC.UI
{
    public partial class FindPrdtPrjtFrm : BaseFrm
    {
        /// <summary>
        /// 选项类型
        /// </summary>
        public GRWL.BZSC.Common.EnumCommon.OptionType CurrentType { get; set; }

        /// <summary>
        /// 当前选择项
        /// </summary>
        public QPrdtPrjt SelectorItem { get; set; }

        /// <summary>
        /// 设定只能搜索已批准的工程
        /// </summary>
        public bool? IsVerify { get; set; }

        /// <summary>
        /// 查询结果临时变量
        /// </summary>
        QPrdtPrjt[] DataAry { get; set; }

        public FindPrdtPrjtFrm(string mtrlCode)
        {
            InitializeComponent();
            this.BaseDataProxy = new GRWL.BZSC.UI.BaseDataSvc.BaseDataClient();
            this.txtBoxMtrlCode.Text = mtrlCode;
        }

        private void FindPrdtPrjtFrm_Load(object sender, EventArgs e)
        {
            this.BaseDataProxy.Open();
            Common.BindEnumToComboBox(this.cbBoxSubmitStatus, typeof(GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus));
            Common.BindEnumToComboBox(this.cbBoxVerifyStatus, typeof(GRWL.BZSC.Common.EnumCommon.PrdtPrjtVerifyStatus));
            this.CurrentType = GRWL.BZSC.Common.EnumCommon.OptionType.物料分类;
            bindTree(this.uTreeView1);
            bindDgv(null, null, null);
        }

        private void bindDgv(int? category, int? depotCategory, int? depot)
        {
            short? submitStatus = null;
            if (!string.IsNullOrEmpty(this.cbBoxSubmitStatus.Text))
                submitStatus = (short)(GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus)Enum.Parse(typeof(GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus), this.cbBoxSubmitStatus.Text, true);
            short? verifyStatus = null;
            if (!string.IsNullOrEmpty(this.cbBoxVerifyStatus.Text))
                verifyStatus = (short)(GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus)Enum.Parse(typeof(GRWL.BZSC.Common.EnumCommon.PrdtPrjtSubmitStatus), this.cbBoxVerifyStatus.Text, true);
            this.dataGridView1.Rows.Clear();
            this.DataAry = this.BaseDataProxy.FindPrdtPrjt(this.IsVerify, category, depotCategory, depot, this.txtBoxCode.Text.Trim(), this.txtBoxVersion.Text.Trim(), this.txtBoxEngineer.Text.Trim(),
                submitStatus, verifyStatus, this.txtBoxMtrlCode.Text.Trim(), this.txtBoxMtrlName.Text.Trim());
            if (DataAry != null)
            {
                foreach (QPrdtPrjt p in DataAry)
                {
                    this.dataGridView1.Rows.Add(new object[] { p.Id, p.Code, p.MaterialCode, p.MaterialName, p.EngineerName, p.DepartmentName, p.CreateTime, p.UpdateTime, p.Hours, p.IsSubmit, p.SubmitManName, p.IsVerify, p.AssessorName });
                }
            }
        }

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            this.dataGridView1.Rows[e.RowIndex].HeaderCell.Value = Convert.ToString(e.RowIndex + 1);//显示行号，也可以设置成显示其他信息 
        }

        private void chose()
        {
            if (this.dataGridView1.SelectedRows != null && this.DataAry != null)
            {
                this.SelectorItem = this.DataAry.Single(p => p.Id == Convert.ToInt32(this.dataGridView1.SelectedRows[0].Cells[0].Value));
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            chose();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            chose();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            search();
        }

        private void search()
        {
            int? category = null;
            int? depotCategory = null;
            int? depot = null;
            if (this.uTabControl1.SelectedIndex == 1)
            {
                if (this.uTreeView2.SelectedNode != null && !string.IsNullOrEmpty(this.uTreeView2.SelectedNode.Name))
                {
                    int id = Convert.ToInt32(this.uTreeView2.SelectedNode.Name);
                    if (this.uTreeView2.SelectedNode.ImageIndex == 3)
                        depot = id;
                    else if (this.uTreeView2.SelectedNode.ImageIndex == 2)
                        depotCategory = id;
                }
            }
            else
            {
                if (this.uTreeView1.SelectedNode != null && !string.IsNullOrEmpty(this.uTreeView1.SelectedNode.Name))
                    category = Convert.ToInt32(this.uTreeView1.SelectedNode.Name);
            }

            bindDgv(category, depotCategory, depot);
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void uTabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.uTabControl1.SelectedIndex == 1)
            {
                this.CurrentType = GRWL.BZSC.Common.EnumCommon.OptionType.仓库分类;
                bindTree(this.uTreeView2);
            }
            else
            {
                this.CurrentType = GRWL.BZSC.Common.EnumCommon.OptionType.物料分类;
                bindTree(this.uTreeView1);
            }
        }

        private void bindTree(UserCtrl.UTreeView tv)
        {
            TB_Options[] options = this.BaseDataProxy.GetOptionTree(this.CurrentType.ToString());
            Common.BindOptionTree(tv, options, this.CurrentType.ToString());
            if (this.CurrentType == GRWL.BZSC.Common.EnumCommon.OptionType.仓库分类)
            {
                TB_Depot[] ary = this.BaseDataProxy.FindDepots(string.Empty, string.Empty, null);
                if (ary != null)
                    loadDepot(ary);
            }
        }

        private void loadDepot(TB_Depot[] ary)
        {
            foreach (TreeNode node in this.uTreeView2.Nodes[0].Nodes)
            {
                if (node.ImageIndex == 2)
                {
                    int category = Convert.ToInt32(node.Name);
                    var query = from q in ary where q.type == category select q;
                    if (query != null)
                    {
                        foreach (TB_Depot d in query)
                        {
                            TreeNode child = new TreeNode();
                            child.Name = d.id.ToString();
                            child.Tag = d.code;
                            child.Text = "[" + d.code + "]" + d.name;
                            child.ImageIndex = 3;
                            node.Nodes.Add(child);
                        }
                    }
                }
            }
        }

        private void uTreeView2_AfterSelect(object sender, TreeViewEventArgs e)
        {
            search();
        }

        private void uTreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            search();
        }
    }
}
